<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Products extends CI_Controller {
    private $id;

    public function __construct() {
        parent::__construct();
        $this->check();
        $this->load->helper('url');
        $this->load->model('products_m');
        $this->load->model('admin/admin_m');
    }
    
    public function check(){
        $this->id = $this->session->userdata('user_id');
        if(empty($this->id)){
            redirect('admin/user/login');
        }
    }
    
    public function index(){
        $data['active'] = 1;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Products'=>'products','List'=>'products'));
        $data['products'] = $this->products_m->get_products();
        $data['page'] = 1;
        $data['count'] = $this->products_m->get_products_count();
        $data['start'] = 0;
        $data['content'] = $this->load->view('admin/products/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function page($page_number=1){
        $data['active'] = 1;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Products'=>'products','List'=>'products'));
        $data['products'] = $this->products_m->get_products(null,$page_number-1);
        $data['page'] = $page_number;
        //$data['page'] = 1;
        $data['count'] = $this->products_m->get_products_count();
        $data['start'] = ($page_number-1)*20;
        $data['content'] = $this->load->view('admin/products/list_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add(){
        $this->load->helper('form');
        $data['active'] = 1;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Products'=>'products','List'=>'products','Add'=>'products/add'));
        $data['parent_cat'] = $this->products_m->get_categories();
        $data['products_cat'] = $this->products_m->get_products_cat();
        $data['designers'] = $this->products_m->get_designers_list();
        $data['content'] = $this->load->view('admin/products/add_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function add_submit(){
        $input['name'] = $this->input->post('name');
        $input['category'] = $this->input->post('category');
        $input['price'] = $this->input->post('price');
        $input['quantity'] = $this->input->post('quantity');
        $input['designer'] = $this->input->post('designer');
        $input['dimention'] = $this->input->post('dimention');
        $input['materials'] = $this->input->post('materials');
        $input['year'] = $this->input->post('year');
        $input['qty_status'] = $this->input->post('qty_status');
        $input['description'] = $this->input->post('description');
        if ($_FILES["main_photo"]["error"] > 0) {
            $this->session->set_flashdata('f_products','Photo error. Please try again');
            redirect('admin/products/add');
        } else if(strlen($_FILES["main_photo"]["name"]) == 0){
            $this->session->set_flashdata('f_products','Photo error. Please upload a photo');
            redirect('admin/products/add');
        } else {
            $name = time().preg_replace('/\s+/', '_', $_FILES["main_photo"]["name"]);
            move_uploaded_file($_FILES["main_photo"]["tmp_name"], "./photo/products/main/" . $name);
            $input['main_photo'] = $name;
            $this->products_m->add($input);
            $this->session->set_flashdata('f_products','Product has been added. Click photo to add some photo to product');
            redirect('admin/products');
        }
    }
    
    public function delete($id){
        $this->products_m->delete($id);
        $this->session->set_flashdata('f_products','Product has been deleted');
        redirect('admin/products');
    }
    
    public function edit($id){
        $this->load->helper('form');
        $data['active'] = 1;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Products'=>'products','List'=>'products','Edit'=>'products/edit'));
        $data['parent_cat'] = $this->products_m->get_categories();
        $data['designers'] = $this->products_m->get_designers_list();
        $data['product'] = $this->products_m->get_product($id);
        //$data['list_cat'] = $this->products_m->get_categories($data['product']->products_cat_parent);
        $data['products_cat'] = $this->products_m->get_products_cat();
        $data['content'] = $this->load->view('admin/products/edit_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    public function edit_submit(){
        $id = $this->input->post('id');
        $input['id'] = $this->input->post('id');
        $input['name'] = $this->input->post('name');
        $input['category'] = $this->input->post('category');
        $input['price'] = $this->input->post('price');
        $input['quantity'] = $this->input->post('quantity');
        $input['designer'] = $this->input->post('designer');
        $input['dimention'] = $this->input->post('dimention');
        $input['materials'] = $this->input->post('materials');
        $input['year'] = $this->input->post('year');
        $input['qty_status'] = $this->input->post('qty_status');
        $input['description'] = $this->input->post('description');
        if(strlen($_FILES["main_photo"]["name"]) > 0){
            if ($_FILES["main_photo"]["error"] > 0) {
                $input['main_photo'] = null;
            } else {
                $name = time() . preg_replace('/\s+/', '_', $_FILES["main_photo"]["name"]);
                move_uploaded_file($_FILES["main_photo"]["tmp_name"], "./photo/products/main/" . $name);
                $input['main_photo'] = $name;
            }
        }
        $this->session->set_flashdata('f_products','Product has been edited.');
        $this->products_m->edit($input);
        redirect('admin/products/edit/'.$id);
    }
    
    public function edit_photo($id){
        $data['active'] = 1;
        $data['breadcumb'] = $this->admin_m->get_breadcumb(array('Products'=>'products','List'=>'products','Edit Photo'=>'products/edit_photo/'.$id));
        $data['product'] = $this->products_m->get_product($id);
        $data['photos'] = $this->products_m->get_product_photo($id);
        $data['content'] = $this->load->view('admin/products/photo_v',$data,TRUE);
        $this->load->view('admin/admin_v',$data);
    }
    
    //MANAGE PHOTO\
    public function photo_add(){
        $input['products_id'] = $this->input->post('products_id');
        if ($_FILES["photo"]["error"] > 0) {
            die();
            $this->session->set_flashdata('f_products_photo','Photo error. Please try again.');
            redirect('admin/products/edit_photo/'.$input['products_id']);
        } else if(strlen($_FILES["photo"]["name"]) == 0){
            $this->session->set_flashdata('f_products_photo','Photo error. Please upload a photo.');
            redirect('admin/products/edit_photo/'.$input['products_id']);
        } else {
            $name = time().preg_replace('/\s+/', '_', $_FILES["photo"]["name"]);
            move_uploaded_file($_FILES["photo"]["tmp_name"], "./photo/products/" . $name);
            $input['photo'] = $name;
            $this->products_m->photo_add($input);
            $this->session->set_flashdata('f_products_photo','Photo has been added.');
            redirect('admin/products/edit_photo/'.$input['products_id']);
        }
    }
    
    public function photo_delete($products_id,$id){
         $this->products_m->photo_delete($id);
         $this->session->set_flashdata('f_products_photo','Photo has been deleted.');
         redirect('admin/products/edit_photo/'.$products_id);
    }
    
    //ADDITIONAL
    public function get_categories($parent_id){
        $list = $this->products_m->get_categories($parent_id);
        foreach($list->result() as $p){
            echo "<option value=\"".$p->products_cat_id."\">".$p->products_cat_title."</option>";
        }
    }
}